package cs342.group1.messenger;

import com.example.grouponemessenger.R;
import com.example.grouponemessenger.R.layout;
import com.example.grouponemessenger.R.menu;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class Login extends Activity {
	private Database db; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		db = new Database(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}
	
	
	public void Login(View view)
	{
		EditText usernameText = (EditText)findViewById(R.id.enter_username);
		EditText passwordText = (EditText)findViewById(R.id.enter_password);
		EditText errorText = (EditText)findViewById(R.id.error_message);
		final String username = usernameText.getText().toString();
		final String password = passwordText.getText().toString();
		
		CheckCloudUsernamePassword task = new CheckCloudUsernamePassword();
		task.execute(username, password);
		int check = -1;
		try {
			check = task.get();
		}
		catch (Exception e) {
			check = -1;
		}
		
		if(check > -1){
			errorText.setText("Success");
			db.open();
			db.InsertNewCloudUser(check, username);
			Intent loginIntent = new Intent(Login.this,MainActivity.class);
	    	startActivity(loginIntent);
		}else{
			errorText.setText("Username and Password do not match");
		}
		
		
	}
	
	public void Register(View view)
	{
		EditText usernameText = (EditText)findViewById(R.id.enter_username);
		EditText passwordText = (EditText)findViewById(R.id.enter_password);
		EditText errorText = (EditText)findViewById(R.id.error_message);
		final String username = usernameText.getText().toString();
		final String password = passwordText.getText().toString();
		
		//Call to check if the username already exists for not
		CheckCloudUsername task = new CheckCloudUsername();
		task.execute(username);
		boolean isInUse = true;
		try {
			isInUse = task.get();
		}
		catch (Exception e) {
			isInUse = true;
		}
		
		//Now do something depending if the username is unique or not
		if(isInUse){
			//username is already in use, display error message here
			errorText.setText("This username is already in use");
		}else{
			//register the new user with the username and password
			errorText.setText("Success");
			InsertNewUser task2 = new InsertNewUser();
			int cloudId = -1;
			String salt = PasswordHasher.getSalt();
			String hashedPW = PasswordHasher.md5(password + salt);
			task2.execute(username, hashedPW, salt);
			try {
				cloudId = task2.get();
			}
			catch (Exception e) {
				cloudId = -1;
			}
			if(cloudId > -1){
				db.open();
				db.InsertNewCloudUser(cloudId, username);
			}
			else{
				errorText.setText("Registration failed! Try agian later.");
			}
			Intent loginIntent = new Intent(Login.this,MainActivity.class);
	    	startActivity(loginIntent);
		}
	}
	
	
	/*
	 * Async calls to the cloud helper class
	 */
	
	public class CheckCloudUsername extends AsyncTask<String, Void, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {
			return (Boolean)(new CloudHelper().CheckUsernameExists(params[0]));
		}
	}
	
	public class InsertNewUser extends AsyncTask<String, Void, Integer> {
		@Override
		protected Integer doInBackground(String... params) {
			return new CloudHelper().InsertNewUser(params[0], params[1], params[2]);
		}
	}
	
	public class CheckCloudUsernamePassword extends AsyncTask<String, Void, Integer> {
		@Override
		protected Integer doInBackground(String... params) {
			int id = (new CloudHelper().CheckUsernamePasswordValid(params[0], params[1]));
			return id;
		}
	}

}
